/** @file               sw_socket.h
 *  @copyright          Copyright (C) 2006-2026 flyfish (tenggui_fu@126.com)
 *  @brief              This file is part of the swallow library
 *  @author             $Author: flyfish $
 *  @version            $Rev: 385 $
 *  @date               $Date: 2016-12-22 00:12:05 +0800 (周四, 22 12月 2016) $
 */
#ifndef __SW_SOCKET_H__
#define __SW_SOCKET_H__

#ifdef __cplusplus
extern "C" {
#endif

#ifdef WIN32
typedef SOCKET SW_SOCKET;
#define SW_INVALID_SOCKET (SW_SOCKET)INVALID_SOCKET
#else
typedef int SW_SOCKET;
#define SW_INVALID_SOCKET (SW_SOCKET)(-1)
#endif

typedef enum _SW_SOCKET_TYPE
{
    SW_ST_TCP,
    SW_ST_UDP,
    SW_ST_NUM
}SW_SOCKET_TYPE;

SW_API SW_RET SW_InitSocketAPI(SW_VOID);
SW_API SW_RET SW_RunSocketAPI(SW_VOID);
SW_API SW_RET SW_StopSocketAPI(SW_VOID);
SW_API SW_RET SW_ExitSocketAPI(SW_VOID);
SW_API SW_SOCKET SW_CreateSocket(SW_SOCKET_TYPE type);
SW_API SW_RET SW_CloseSocket(SW_SOCKET sd);
SW_API SW_RET SW_BindSocket(SW_SOCKET sd, const SW_CHAR *pAddr, SW_UINT16 port);
SW_API SW_RET SW_ListenSocket(SW_SOCKET sd, SW_UINT32 backLog);
SW_API SW_SOCKET SW_AcceptSocket(SW_SOCKET sd, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort);
SW_API SW_SOCKET SW_TimeAcceptSocket(SW_SOCKET sd, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort, SW_UINT32 msec);
SW_API SW_RET SW_ConnectSocket(SW_SOCKET sd, const SW_CHAR *pAddr, SW_UINT16 port);
SW_API SW_RET SW_TimeConnectSocket(SW_SOCKET sd, const SW_CHAR *pAddr, SW_UINT16 port, SW_UINT32 msec);
SW_API SW_RET SW_GetSocketLocalAddrPort(SW_SOCKET sd, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort);
SW_API SW_RET SW_GetSocketRemoteAddrPort(SW_SOCKET sd, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort);
SW_API SW_BOOL SW_IsSocketReadable(SW_SOCKET sd);
SW_API SW_BOOL SW_IsSocketWritable(SW_SOCKET sd);
SW_API SW_RET SW_WaitSocketReadable(SW_SOCKET sd);
SW_API SW_RET SW_WaitSocketWritable(SW_SOCKET sd);
SW_API SW_RET SW_TimeWaitSocketReadable(SW_SOCKET sd, SW_UINT32 msec);
SW_API SW_RET SW_TimeWaitSocketWritable(SW_SOCKET sd, SW_UINT32 msec);
SW_API SW_UINT32 SW_GetSocketRecvBufSize(SW_SOCKET sd);
SW_API SW_RET SW_SetSocketRecvBufSize(SW_SOCKET sd, SW_UINT32 size);
SW_API SW_UINT32 SW_GetSocketSendBufSize(SW_SOCKET sd);
SW_API SW_RET SW_SetSocketSendBufSize(SW_SOCKET sd, SW_UINT32 size);
SW_API SW_UINT32 SW_GetSocketRecvTimeout(SW_SOCKET sd);
SW_API SW_RET SW_SetSocketRecvTimeout(SW_SOCKET sd, SW_UINT32 msec);
SW_API SW_UINT32 SW_GetSocketSendTimeout(SW_SOCKET sd);
SW_API SW_RET SW_SetSocketSendTimeout(SW_SOCKET sd, SW_UINT32 msec);
SW_API SW_BOOL SW_IsSocketNoDelayEnabled(SW_SOCKET sd);
SW_API SW_RET SW_EnableSocketNoDelay(SW_SOCKET sd, SW_BOOL enable);
SW_API SW_RET SW_GetSocketLinger(SW_SOCKET sd, SW_BOOL *pEnable, SW_UINT32 *pSec);
SW_API SW_RET SW_SetSocketLinger(SW_SOCKET sd, SW_BOOL enable, SW_UINT32 sec);
SW_API SW_BOOL SW_IsSocketKeepAliveEnabled(SW_SOCKET sd);
SW_API SW_RET SW_EnableSocketKeepAlive(SW_SOCKET sd, SW_BOOL enable);
SW_API SW_BOOL SW_IsSocketNonBlockEnabled(SW_SOCKET sd);
SW_API SW_RET SW_EnableSocketNonBlock(SW_SOCKET sd, SW_BOOL enable);
SW_API SW_BOOL SW_IsSocketReuseAddrEnabled(SW_SOCKET sd);
SW_API SW_RET SW_EnableSocketReuseAddr(SW_SOCKET sd, SW_BOOL enable);
SW_API SW_UINT32 SW_GetSocketDscp(SW_SOCKET sd);
SW_API SW_RET SW_SetSocketDscp(SW_SOCKET sd, SW_UINT32 dscp);
SW_API SW_UINT32 SW_GetSocketTtl(SW_SOCKET sd);
SW_API SW_RET SW_SetSocketTtl(SW_SOCKET sd, SW_UINT32 ttl);
SW_API SW_UINT32 SW_ReadSocket(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_ReadSocketN(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_WriteSocket(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_WriteSocketN(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_RecvSocket(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_RecvSocketN(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_SendSocket(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_SendSocketN(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_TimeReadSocket(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeWriteSocket(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeRecvSocket(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeSendSocket(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeReadSocketN(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeWriteSocketN(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeRecvSocketN(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeSendSocketN(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_RecvFromSocket(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort);
SW_API SW_UINT32 SW_SendToSocket(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, const SW_CHAR *pAddr, SW_UINT16 port);
SW_API SW_UINT32 SW_RecvFromSocketN(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort);
SW_API SW_UINT32 SW_SendToSocketN(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, const SW_CHAR *pAddr, SW_UINT16 port);
SW_API SW_UINT32 SW_TimeRecvFromSocket(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeSendToSocket(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, const SW_CHAR *pAddr, SW_UINT16 port, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeRecvFromSocketN(SW_SOCKET sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_CHAR *pAddr, SW_UINT32 addrLen, SW_UINT16 *pPort, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeSendToSocketN(SW_SOCKET sd, const SW_VOID *pBuf, SW_UINT32 bufLen, const SW_CHAR *pAddr, SW_UINT16 port, SW_UINT32 msec);
SW_API SW_RET SW_GetHostByName(const SW_CHAR *pName, SW_CHAR *pAddr, SW_UINT32 addrLen);
SW_API SW_SOCKET SW_CreateTcpSocketServer(SW_UINT16 port, SW_UINT32 backLog, SW_BOOL nonBlock);
SW_API SW_SOCKET SW_CreateUdpSocketServer(SW_UINT16 port, SW_BOOL nonBlock);
SW_API SW_RET SW_GetAppSocketDir(SW_CHAR *pDirName, SW_UINT32 dirNameLen);

#ifdef __cplusplus
}
#endif

#endif

